import { Component, Input, Output, EventEmitter, OnInit, OnChanges } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { HttpServcie } from '../../../../service/http.service';
import { Config } from '../../../../service/config';
import { SysAuthorBean } from './../bean/sysAuthor.bean';
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

@Component({
    selector: 'function-modify-page',
    templateUrl: 'function-modify.html',
    styleUrls: ['function-modify.scss']
})
export class FunctionModifyPage implements OnInit, OnChanges {
    validateForm: FormGroup;
    isSpinning: boolean=false;
    functions: any = [];
    @Input() inValue: any = {};
    @Output("outPut") outPut = new EventEmitter()
    //系统类型
    searchOptions: any = Config.SYS_TYPE;
    //权限类型
    authorOptions: any = Config.AUTHOR_TYPE;
    constructor(
        private fb: FormBuilder,
        private httpServcie: HttpServcie,
        private _notification: NzNotificationService
    ) {
    }

    ngOnInit() {
        
    }

    ngOnChanges() {
        this.functions = [];
        if (this.inValue) {
            this.functions = this.inValue.authors;
            // this.validateForm = this.fb.group({
            //     authorName: this.inValue.authorName,
            //     sysType: this.inValue.sysType,
            //     authorUrl: this.inValue.authorUrl, 
            //     parentId: this.inValue.parentId,
            //     authorDescribe: this.inValue.authorDescribe
            // });

            this.validateForm = this.fb.group({
                id: [this.inValue.id, [Validators.nullValidator]],
                authorName: [this.inValue.authorName, [Validators.required]],
                sysType: [this.inValue.sysType, [Validators.required]],
                authorUrl: [this.inValue.authorUrl, [Validators.nullValidator]],
                parentId: [this.inValue.parentId, [Validators.nullValidator]],
                authorDescribe: [this.inValue.authorDescribe, [Validators.nullValidator]],
                authorId: [this.inValue.authorId, [Validators.required]],
                authorType: [this.inValue.authorType, [Validators.required]]
            });
        }
    }

    submitForm($event, data) {
        //校验是否能提交
        //没有填的标红
        for (const i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
        }
        //处理是否提交
        for (const i in this.validateForm.controls) {
            if(!this.validateForm.controls[i].valid){
                return;
            }
        }

        this.isSpinning = true;
        let ob = this.httpServcie.updateSystemAuthor(data);
        ob.subscribe(()=>{
            this.isSpinning = false;
            this._notification.create('success', '成功提示', '修改成功');
            this.outPut.emit($event);
        })
        
    }

    //交验
    getFormControl(name) {
        return this.validateForm.controls[name];
    }
}